
package FrameWork;

import utils.TextureLoader;
import com.jogamp.opengl.util.texture.Texture;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;

/**
 *
 * @author trong_000
 */

public class SkyBox {
    
    private float m_size;
    //texture
    private Texture ttTop;
    private Texture ttBottom;
    private Texture ttFront;
    private Texture ttBack;
    private Texture ttLeft;
    private Texture ttRight;

    public SkyBox() {
    }

    public void Initialize(float size) {
        this.m_size = size;
    }

    public void LoadTextures(Texture top, Texture bottom, Texture front, Texture back, Texture left, Texture right) {
        ttTop = top;
        ttBottom = bottom;
        ttFront = front;
        ttBack = back;
        ttLeft = left;
        ttRight = right;
    }

    public void LoadTextures(String top, String bottom, String front, String back, String left, String right) {
        ttTop = TextureLoader.getIns().Load(top);
        ttBottom = TextureLoader.getIns().Load(bottom);
        ttFront = TextureLoader.getIns().Load(front);
        ttBack = TextureLoader.getIns().Load(back);
        ttLeft = TextureLoader.getIns().Load(left);
        ttRight = TextureLoader.getIns().Load(right);
    }
    
    public void render(GL2 gl){
        gl.glPushMatrix();
        gl.glLoadIdentity();
        double size = m_size;

    // ----------------------------
    // D+-----+C  ** ^ Y (0, 1)
    //  |     |   ** |+--+
    //  |     |   ** ||  | TEXTURES
    //  |     |   ** |+--+
    // A+-----+B  ** *----->X (0,1)
    // ----------------------------

    // ---------------------------------------------------
    // culling is done ccw (right hand rule defines front)
    // ---------------------------------------------------
        gl.glEnable(GL2.GL_CULL_FACE);
    // ----------------------------------------------------

        gl.glEnable(GL2.GL_TEXTURE_2D);

        ttBack.bind(gl);

        gl.glBegin(GL2.GL_QUADS);
        gl.glTexCoord2f(0f, 0f);
        gl.glVertex3d(-1 * size, -1 * size, -1 * size); // A
        gl.glTexCoord2f(1f, 0f);
        gl.glVertex3d(+1 * size, -1 * size, -1 * size); // B
        gl.glTexCoord2f(1f, 1f);
        gl.glVertex3d(+1 * size, +1 * size, -1 * size); // C
        gl.glTexCoord2f(0f, 1f);
        gl.glVertex3d(-1 * size, +1 * size, -1 * size); // D
        gl.glEnd();

        ttFront.bind(gl);

        gl.glBegin(GL2.GL_QUADS);
        gl.glTexCoord2f(0f, 0f);
        gl.glVertex3d(+1 * size, -1 * size, +1 * size); // A
        gl.glTexCoord2f(1f, 0f);
        gl.glVertex3d(-1 * size, -1 * size, +1 * size); // B
        gl.glTexCoord2f(1f, 1f);
        gl.glVertex3d(-1 * size, +1 * size, +1 * size); // C
        gl.glTexCoord2f(0f, 1f);
        gl.glVertex3d(+1 * size, +1 * size, +1 * size); // D
        gl.glEnd();

        ttLeft.bind(gl);

        gl.glBegin(GL2.GL_QUADS);
        gl.glTexCoord2f(0f, 0f);
        gl.glVertex3d(+1 * size, -1 * size, -1 * size); // A
        gl.glTexCoord2f(1f, 0f);
        gl.glVertex3d(+1 * size, -1 * size, +1 * size); // B
        gl.glTexCoord2f(1f, 1f);
        gl.glVertex3d(+1 * size, +1 * size, +1 * size); // C
        gl.glTexCoord2f(0f, 1f);
        gl.glVertex3d(+1 * size, +1 * size, -1 * size); // D
        gl.glEnd();

        ttRight.bind(gl);

        gl.glBegin(GL2.GL_QUADS);
        gl.glTexCoord2f(0f, 0f);
        gl.glVertex3d(-1 * size, -1 * size, +1 * size); // A
        gl.glTexCoord2f(1f, 0f);
        gl.glVertex3d(-1 * size, -1 * size, -1 * size); // B
        gl.glTexCoord2f(1f, 1f);
        gl.glVertex3d(-1 * size, +1 * size, -1 * size); // C
        gl.glTexCoord2f(0f, 1f);
        gl.glVertex3d(-1 * size, +1 * size, +1 * size); // D
        gl.glEnd();

        ttBottom.bind(gl);

        gl.glBegin(GL2.GL_QUADS);
        gl.glTexCoord2f(0f, 0f);
        gl.glVertex3d(-1 * size, -1 * size, +1 * size); // A
        gl.glTexCoord2f(1f, 0f);
        gl.glVertex3d(+1 * size, -1 * size, +1 * size); // B
        gl.glTexCoord2f(1f, 1f);
        gl.glVertex3d(+1 * size, -1 * size, -1 * size); // C
        gl.glTexCoord2f(0f, 1f);
        gl.glVertex3d(-1 * size, -1 * size, -1 * size); // D
        gl.glEnd();

        ttTop.bind(gl);

        gl.glBegin(GL2.GL_QUADS);
        gl.glTexCoord2f(0f, 0f);
        gl.glVertex3d(-1 * size, +1 * size, -1 * size); // C
        gl.glTexCoord2f(1f, 0f);
        gl.glVertex3d(+1 * size, +1 * size, -1 * size); // D
        gl.glTexCoord2f(1f, 1f);
        gl.glVertex3d(+1 * size, +1 * size, +1 * size); // A
        gl.glTexCoord2f(0f, 1f);
        gl.glVertex3d(-1 * size, +1 * size, +1 * size); // B
        gl.glEnd();

        gl.glPopMatrix();
    }            
}
